package io.renren.project.demo2.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.renren.modules.wyzs.mobile_BillAdapterMoudle.BillAdpater;
import io.renren.modules.wyzs.mobile_BillAdapterMoudle.BillSaver;
import io.renren.modules.wyzs.mobile_BillAdapterMoudle.BillTemp;
import io.renren.modules.wyzs.mobile_BillAdapterMoudle.FieldTemp;
import io.renren.modules.wyzs.service.FactoryService;
import io.renren.modules.wyzsphone.commWithWCF.BillTempGeter;
import io.renren.modules.wyzsphone.commWithWCF.CommWithWCF;
import io.renren.modules.wyzsphone.commWithWCF.XModeRtn;
import io.renren.modules.wyzsphone.commWithWCF.XModeSnd;
import io.renren.modules.wyzsphone.utils.Utils;
import io.renren.project.demo2.service.Demo2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;


@Service
public class Demo2ServiceImpl implements Demo2Service {

    @Autowired
    FactoryService factoryService;

    @Override
    public Map getAllOrder(Map<String, Object> params){
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter = new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X10");//todo
        Value.setMethod("W_GetSalesOrderList");
        //Value.setMethod("M_GetSalesOrder");//M_GetSalesAccountList  M_GetSalesOrder
        Value.setUID((String) params.get("uid"));
        String itemid=(String)params.get("itemid");
        if(itemid!=null&&itemid.equals("")) {
            Value.getData().setData("itemid", itemid);
        }else {
            Value.getData().setData("itemid", "");
        }
        String itemtype=(String)params.get("itemtype");
        if(itemid!=null&&itemid.equals("")) {
            Value.getData().setData("itemtype", itemtype);
        }else {
            Value.getData().setData("itemtype", "");
        }
        Value.getData().setData("currentpage", (String)params.get("currentpage"));//todo
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list=JsonToList(_gson,ValueRtn);
        Map map=new HashMap();
        map.put("list",list);
        return map;

    }

    //暂时用不到
    @Override
    public Map getAllSalesCarByorder(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);

        Value.setFile("X10");
        Value.setMethod("W_GetDetailSalesOrder");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("type", "SalesByOrder");
        Value.getData().setData("currentpage", (String)params.get("currentpage"));//todo
        Value.getData().setData("assid", (String)params.get("assid"));//todo
        Value.getData().setData("orderid", (String)params.get("orderid"));//todo
        Value.getData().setData("orderassid", (String)params.get("orderassid"));//todo

        Double d= (Double) params.get("organizeid");
        DecimalFormat df = new DecimalFormat("######0"); //四色五入转换成整数
        Integer i= Integer.parseInt(df.format(d));
        Value.getData().setData("organizeid", i);//todo

        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        Map maps=JsonToMap(_gson,ValueRtn);
        maps.put("list",maps);
        return maps;
    }

    @Override
    public Map getAppOrderList(Map<String, Object> params){
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");//todo
        Value.setMethod("GetAppOrderList");//获得线上订单
        Value.setUID((String) params.get("uid"));
        Utils.setDoubleToInt(params,"showclose");
        Value.getData().setData("showclose", (Integer) params.get("showclose"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list=Utils.JsonToList(ValueRtn.getJsonObject());
        Map map=new HashMap();
        map.put("list",list);
        map.put("state",ValueRtn.getState());
        return map;
    }

    @Override
    public Map getSalesCarList(Map<String, Object> params){

        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter = new BillTempGeter();
        String url = factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");
        Value.setMethod("GetSalesCarList");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("orderbillno",(String)params.get("orderBillNo"));
        Value.getData().setData("starttime",(String)params.get("startTime"));
        Value.getData().setData("endtime",(String)params.get("endTime"));
//        Utils.setDoubleToInt(params,"showclose");
//        Value.getData().setData("showclose", (Integer) params.get("showclose"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list=Utils.JsonToList(ValueRtn.getJsonObject());
        Map map=new HashMap();
        map.put("list",list);
        map.put("state",ValueRtn.getState());
        return map;

    }

    @Override
    public Map getQtySumByItem(Map<String, Object> params){

        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter = new BillTempGeter();
        String url = factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");
        Value.setMethod("GetQtySumByItem");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("starttime",(String)params.get("startTime"));
        Value.getData().setData("endtime",(String)params.get("endTime"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list=Utils.JsonToList(ValueRtn.getJsonObject());
        Map map=new HashMap();
        map.put("list",list);
        map.put("state",ValueRtn.getState());
        return map;

    }


    @Override
    public Map getNumber(Map<String, Object>params){
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter = new BillTempGeter();
        String url = factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("M00");
        Value.setMethod("GetNumber");
        Value.setUID((String)params.get("uid"));
        Value.getData().setData("classtype","Sales");
        Value.getData().setData("organizeid",(Integer)params.get("organizeId"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        String ValueRtn = ser.Code2(Value);
        Map<String, Object> maps = _gson.fromJson(ValueRtn, new TypeToken<Map<String,Object>>(){}.getType());
        return maps;
    }

    @Override
    public Map getSalesOrderListByOrderID(Map<String, Object>params){

        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter = new BillTempGeter();
        String url = factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X10");
        Value.setMethod("W_GetSalesOrderByOrderID");
        Value.setUID((String)params.get("uid"));
        Value.getData().setData("fid", (String)params.get("fid"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list= Utils.JsonToList(ValueRtn.getJsonObject());
        Map maps=new HashMap();
        maps.put("list",list);
        maps.put("state",ValueRtn.getState());

        return maps;

    }

    @Override
    public Map saveBill(Map<String, Object> params){
        String uid = (String) params.get("uid");
        uid = uid.replaceAll("\"","");
        params.remove("uid");
        params.put("uid",uid);
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();

        try{
        BillSaver billSaver=getBillSaver(params);
        billSaver.setCalssType("AppSalesOrder");
        billSaver.setStatus("New");
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");
        Value.setMethod("SaveBill");
        Value.setUID(uid);
        Value.setJsonObject(_gson.toJson(billSaver));
        //Value.setJsonObject("{\"CalssType\":\"Sales\",\"ID\":0,\"Logs\":[],\"NeedSave\":true,\"NewStamp\":\"352a60cf-2dee-456-9aee-acd46ee41e9a\",\"SaveEntrys\":{\"1\":{\"Page\":1,\"Rows\":[{\"DataExecType\":\"Insert\",\"Fields\":{\"439\":{},\"272\":{\"NewValue\":\"\"},\"26\":{\"NewValue\":1.0},\"576\":{\"NewValue\":\"000.004\"},\"19\":{\"NewValue\":\"000017\"},\"112\":{\"NewValue\":\"000.004\"},\"24\":{\"NewValue\":646.0},\"17\":{\"NewValue\":\"6\"},\"21\":{\"NewValue\":\"成武金利宏新能源有限公司\"},\"213\":{\"NewValue\":1000.0},\"438\":{}},\"Index\":0,\"RowID\":0}]}},\"SaveItems\":{\"33\":{\"NewValue\":6},\"53\":{},\"1\":{\"NewValue\":\"000167\"},\"462\":{},\"62\":{\"NewValue\":\"\"},\"50\":{},\"6\":{\"NewValue\":\"01.01.002\"},\"56\":{},\"41\":{},\"200\":{},\"575\":{},\"238\":{},\"42\":{},\"60\":{\"NewValue\":0.0},\"58\":{\"NewValue\":\"成武金利宏新能源有限公司\"},\"52\":{},\"55\":{},\"46\":{},\"14\":{\"NewValue\":0},\"51\":{},\"7\":{\"NewValue\":0},\"177\":{},\"2\":{},\"8\":{\"NewValue\":1.0},\"44\":{},\"49\":{\"NewValue\":\"00006\"},\"501\":{},\"30\":{\"NewValue\":0},\"5\":{\"NewValue\":\"石灰石\"},\"43\":{},\"47\":{},\"3\":{\"NewValue\":\"鲁254\"},\"4\":{\"NewValue\":\"1\"},\"57\":{},\"461\":{},\"175\":{},\"61\":{\"NewValue\":\" \"},\"59\":{\"NewValue\":\"000.004\"},\"456\":{},\"275\":{},\"176\":{},\"39\":{\"NewValue\":0},\"9\":{},\"10\":{\"NewValue\":0},\"327\":{}},\"Status\":\"New\"}");
        XModeRtn ValueRtn = ser.Code(Value);

        // String ValueRtn3 = ser.Code3(Value);
        Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>() {
        }.getType());
        if(maps==null){
            maps=new HashMap();
        }
        maps.put("billno",billSaver.getSaveItems());
        maps.put("state",ValueRtn.getState());
        //zcx
        String msg = (String)maps.get("Message");
        Map<String, Object> maps2 = _gson.fromJson(ValueRtn.getMessage(), new TypeToken<Map<String, Object>>(){}.getType());

        maps.put("msg",maps2.get("Message")+msg);

        Double d =(Double)maps.get("ID");
        Integer i=d.intValue();
        maps.remove("ID");
        maps.put("ID",i);
        return maps;

        }catch(Exception e){
            e.printStackTrace();
            Map<String, Object> maps = new HashMap<String, Object>();
            maps.put("state",1);
            maps.put("msg","java运行错误："+e.toString());
            return maps;
        }
    }

    private BillSaver getBillSaver(Map<String, Object> params){

        BillTemp _BillTemp = getBillTemp(params);
        BillAdpater _Adapter = new BillAdpater(_BillTemp);
        FieldTemp temp=new FieldTemp();
        temp.setNeedSave(true);
        _Adapter.CharStamp=params.get("guid").toString();
        Double valueDouble= (Double) params.get("organizeId");
        Integer valueInt=valueDouble.intValue();
        params.remove("organizeId");
        params.put("organizeId",valueInt);
        _Adapter.Fields.get("FOrderID").setValue(params.get("FOrderID"));
        _Adapter.Fields.get("FBillNo").setValue(getBillNo(params));//有
        _Adapter.Fields.get("FOrderAssID").setValue(params.get("FOrderAssID")); //订单辅助ID 和FOrderID一样 都是订单带过来的
        _Adapter.Fields.get("FOrderNumber").setValue(params.get("FOrderNumber"));
        _Adapter.Fields.get("FOrderType").setValue(2);  //暂时=1 以后要根据选择情况调整 todo   zcx 为1是金额式 为2是数量式
        _Adapter.Fields.get("FItemID").setValue(params.get("FItemID"));
        _Adapter.Fields.get("FItemNumber").setValue(params.get("FItemNumber"));
        _Adapter.Fields.get("FItemName").setValue(params.get("FItemName"));
        //  _Adapter.Fields.get("FOrderName").setValue(params.get("FOrderName"));  todo
        _Adapter.Fields.get("FCancel").setValue(0);
        _Adapter.Fields.get("FClosed").setValue(0);

        _Adapter.Fields.get("FOrganizeID").setValue(params.get("organizeId"));
        _Adapter.Fields.get("FCustomID").setValue(params.get("FCustomID"));
        _Adapter.Fields.get("FCustomName").setValue(params.get("FCustomName"));
        _Adapter.Fields.get("FCustomNumber").setValue(params.get("FCustomNumber"));
        _Adapter.Fields.get("FQty").setValue(params.get("FQty"));
        _Adapter.Fields.get("FPrice").setValue(params.get("FPrice"));

        //转换日期格式 否则保存不到数据库 Tue Aug 28 2018 13:07:18 GMT+0800 (中国标准时间)转换成 yyyy-mm-dd hh:mm:ss
//        String strStartIime = ConvertDateTime((String) params.get("FStartTime"));
//        _Adapter.Fields.get("FStartTime").setValue(strStartIime);
//
//        String strEndIime = ConvertDateTime((String) params.get("FEndTime"));
//        _Adapter.Fields.get("FEndTime").setValue(strEndIime);

        _Adapter.Fields.get("FStartTime").setValue(params.get("FStartTime"));
        _Adapter.Fields.get("FEndTime").setValue(params.get("FEndTime"));

        _Adapter.Fields.get("FMultiple").setValue(params.get("FMultiple"));

        return _Adapter.GetSaveItem();

    }


  private String ConvertDateTime(String strTime){

      try {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
          Date startDate = new Date(strTime);
          String strConvertIime = sdf.format(startDate);
          return strConvertIime;

      }catch(Exception e){
          e.printStackTrace();
          return "";
      }
  }

    private BillTemp getBillTemp(Map<String, Object> params){
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter = new BillTempGeter();
        String url = factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("M00");
        Value.setMethod("GetBillTemp");
        Value.setUID((String) params.get("uid"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType("AppSalesOrder");
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        BillTemp billTemp = _gson.fromJson(ValueRtn.getJsonObject(),BillTemp.class);
        return billTemp;
    }

    private String getBillNo(Map<String, Object> params) {
        Map map=getNumber2(params);
        Map b= (Map) map.get("Data");
        Map c= (Map) b.get("Data");
        return  c.get("billno").toString();
        //return  "0000125";

    }

    /**
     * 获得number
     */
    public Map getNumber2(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("M00");
        Value.setMethod("GetNumber");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("classtype", "AppSalesOrder");
        Value.getData().setData("organizeid", ((Integer)params.get("organizeId")));//todo
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        String ValueRtn = ser.Code2(Value);
        Map<String, Object> maps = _gson.fromJson(ValueRtn, new TypeToken<Map<String, Object>>() {
        }.getType());

        return maps;
    }


    //给批量下单增加车辆
    @Override
    public Map AppCarListAdd(Map<String, Object>params, String jsonStr)
    {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");//todo
        Value.setMethod("AppCarListAdd");//W_GetSalesEmpowerList M_GetSalesTaskCar
        Value.setJsonObject(jsonStr);
        Value.setUID((String) params.get("uid"));
        Utils.setDoubleToInt(params,"BillID");
        Value.getData().setData("BillID", (Integer)params.get("BillID"));
        Value.getData().setData("CarNo", (String)params.get("CarNo"));
        Value.getData().setData("Qty", (String)params.get("Qty"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        XModeRtn ValueRtn = ser.Code(Value);
        // Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>(){}.getType());
        Map maps=new HashMap();
        maps.put("state",ValueRtn.getState());
        return maps;
    }

    @Override
    public Map AppCarListDel(Map<String, Object>params, String jsonStr){
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");//todo
        Value.setMethod("AppCarListDel");//W_GetSalesEmpowerList M_GetSalesTaskCar
        //Value.setJsonObject(jsonStr);
        Value.setUID((String) params.get("uid"));
        Utils.setDoubleToInt(params,"RowID");
        Value.getData().setData("rowid", (Integer)params.get("RowID"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        XModeRtn ValueRtn = ser.Code(Value);
        // Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>(){}.getType());
        Map maps=new HashMap();
        maps.put("state",ValueRtn.getState());
        return maps;
    }


    @Override
    public Map getAppOrderBill(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");
        Value.setMethod("GetAppOrderBill");
        Value.setUID((String) params.get("uid"));

        Utils.setDoubleToInt(params,"ID");
        Value.getData().setData("id", (Integer)params.get("ID"));//todo
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>(){}.getType());
        if(maps==null){
            maps = new HashMap();
        }
        maps.put("state",ValueRtn.getState());
        //返回消息
        Map<String, Object> maps2 = _gson.fromJson(ValueRtn.getMessage(), new TypeToken<Map<String, Object>>(){}.getType());
        maps.put("msg",maps2.get("Message"));
        return maps;
    }

    @Override
    public Map getAppOrderCarInfo(Map<String,Object>params){
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter = new BillTempGeter();
        String url = factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X05");
        Value.setMethod("GetAppOrderCarInfo");
        Value.setUID((String) params.get("uid"));

        Utils.setDoubleToInt(params,"Code");
        Value.getData().setData("code", (Integer)params.get("Code"));//todo

        Utils.setDoubleToInt(params,"RowID");
        Value.getData().setData("rowid", (Integer)params.get("RowID"));

        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>(){}.getType());
        if(maps==null){
            maps = new HashMap<String,Object>();
        }
        maps.put("state",ValueRtn.getState());
        //返回消息
        Map<String, Object> maps2 = _gson.fromJson(ValueRtn.getMessage(), new TypeToken<Map<String, Object>>(){}.getType());
        maps.put("msg",maps2.get("Message"));
        return maps;
    }


    private Map JsonToMap(Gson _gson,XModeRtn ValueRtn)
    {
        Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>() {
        }.getType());
        for (Map.Entry<String, Object> entry : maps.entrySet()) {
            System.out.println("key: " + entry.getKey() + "  " + "value: " + entry.getValue());

        }
        return maps;
    }
    private List JsonToList(Gson _gson, XModeRtn ValueRtn)
    {
        List<String> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<List<Object>>() {
        }.getType());
        return maps;
    }
}
